import Vue from 'vue'
import App from './App'

/* 引用store */
import store from '@/store'
Vue.prototype.$store = store;
/* 引用store结束 */

/* websokit连接 */
var socketOpen = false;


// var serverPort = '6900'; // webSocket连接端口
// var wsuri = 'wss:ffcy.stqcm.com:' + serverPort;

var serverPort = '6910'; // webSocket连接端口
var wsuri = 'ws:47.110.142.92:' + serverPort;

uni.connectSocket({
	url: wsuri
});

uni.onSocketOpen(function(res) {
	store.commit('setSocketOpen', true)
	store.state.socketOpen = true;

	let curRoutePath = window.location.href;


	const page = window.location.href.split("?");

	if (page[0]) {
		curRoutePath = page[0]
	}
	const curRoute = curRoutePath.split("/pages")[1]
	const adminRouter = ['/backstage/delivery/pick-up', '/backstage/delivery/recycle', '/backstage/delivery/renewal',
		'/backstage/supplier/detail', '/backstage/supplier/platoon', '/backstage/supplier/stocks'
	]
	const interRouter = ['/index/index', '/order/index', '/backstage/delivery/index', '/backstage/supplier/index',
		'/personal/opinion'
	]
	// 刷新保持登录
	uni.getStorage({
		key: 'uerInfo',
		success: function(res) {
			const data = store.state.socketMsgQueue
			if (curRoute === '/scene/index') {
				for (let i = 0; i < data.length; i++) {
					uni.sendSocketMessage({
						data: data[i]
					});
				}
			} else if (interRouter.indexOf(curRoute) !== -1 || !curRoute) {
				// 取出缓存的登录信息先行发送
				for (let i = 0; i < data.length; i++) {
					if (JSON.parse(data[i]).api === "api_wx_user_login" || JSON.parse(data[i]).api === "api_wxadmin_admin_login" ||
						JSON.parse(data[i]).api === "api_wx_user_keepLogin") {
						uni.sendSocketMessage({
							data: store.state.socketMsgQueue[i]
						});
						data.splice(i, 1);
					}
				}
				store.state.socketMsgQueue = data
			} else if (res.data.token) {
				if (adminRouter.indexOf(curRoute) !== -1) {
					const path = {
						app: "wxadmin",
						api: "api_wxadmin_admin_keepLogin",
						version: "1.0",
						type: 2,
						data: {
							token: res.data.token
						}
					}
					uni.sendSocketMessage({
						data: JSON.stringify(path)
					})

				} else {
					const path = {
						app: "wx",
						api: "api_wx_user_keepLogin",
						version: "1.0",
						type: "1",
						data: {
							token: res.data.token
						}
					}
					uni.sendSocketMessage({
						data: JSON.stringify(path)
					})
				}
			} else {
				const data = store.state.socketMsgQueue
				// 免登录名单
				const noLogin = ['api_wx_user_login', 'api_wxadmin_admin_login', 'api_hardware_buy_getGoodsInfo',
					'api_hardware_buy_getInfo', 'api_hardware_buy_createOrder'
				]
				// 取出缓存的登录信息先行发送
				for (let i = 0; i < data.length; i++) {
					if (noLogin.indexOf(JSON.parse(data[i]).api) !== -1) {
						uni.sendSocketMessage({
							data: data[i]
						});
						data.splice(i, 1);
					}
				}
				store.state.socketMsgQueue = data
			}
		},
		fail(erro) {
			const data = store.state.socketMsgQueue
			// 免登录名单
			const noLogin = ['api_wx_user_login', 'api_wxadmin_admin_login', 'api_hardware_buy_getGoodsInfo',
				'api_hardware_buy_getInfo', 'api_hardware_buy_createOrder'
			]
			// 取出缓存的登录信息先行发送
			for (let i = 0; i < data.length; i++) {
				if (noLogin.indexOf(JSON.parse(data[i]).api) !== -1) {
					uni.sendSocketMessage({
						data: data[i]
					});
					data.splice(i, 1);
				}
			}
			store.state.socketMsgQueue = data
		}
	})

});


uni.onSocketError(function(res) {
	console.log('WebSocket连接打开失败，请检查！');
});

uni.onSocketClose(function(res) {
	console.log('WebSocket 已关闭！');
	// 断链重连
	uni.connectSocket({
		url: wsuri
	});
});

/* websokit连接结束 */


import mySocket from "@/api/my-socket";
Vue.use(mySocket); // socket 引用

import type from "@/utils/type";
Vue.use(type); // 引用自己定义的上传文件函数


//监听浏览器关闭事件(绝对好使，需所有代码放到生命周期mounted中)
// var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
// var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器  
// var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
// var isIE11 = userAgent.indexOf("rv:11.0") > -1; //判断是否是IE11浏览器
// var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
// if (!isIE && !isEdge && !isIE11) { //兼容chrome和firefox
// 	var _beforeUnload_time = 0,
// 		_gap_time = 0;
// 	var is_fireFox = navigator.userAgent.indexOf("Firefox") > -1; //是否是火狐浏览器
// 	window.onunload = function() {
// 		_gap_time = new Date().getTime() - _beforeUnload_time;
// 		if (_gap_time <= 5) {
// 			uni.removeStorage({
// 				key: 'uerInfo',
// 				success: function(res) {}
// 			})
// 		} else { //谷歌浏览器刷新
// 		}
// 	}
// 	window.onbeforeunload = function() {
// 		_beforeUnload_time = new Date().getTime();
// 		if (is_fireFox) { //火狐关闭执行
// 			uni.removeStorage({
// 				key: 'uerInfo',
// 				success: function(res) {}
// 			})
// 		} else { //火狐浏览器刷新
// 		}
// 	};
// }


// 查看报错
import VConsole from 'vconsole/dist/vconsole.min.js' //import vconsole
let vConsole = new VConsole() // 初始化

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
